package com.shchuangzhi.smartguard.core;

import com.elvishew.xlog.LogLevel;
import com.elvishew.xlog.printer.Printer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by j_hu on 5/12/17.
 */

public class NotifyPrinter implements Printer, Notify {
    private HashMap<Integer, Notify> notifyMap;
    private static final NotifyPrinter instance = new NotifyPrinter();

    private NotifyPrinter() {
        this.notifyMap = new HashMap<>();
    }

    @Override
    public void println(int logLevel, String tag, String msg) {
        NotifyMessage message = logLevel == LogLevel.ERROR ?
                NotifyMessage.error(msg, Consts.NOTIFY_DATA_SYC_VIEW_LIST) : NotifyMessage.info(msg, Consts.NOTIFY_DATA_SYC_VIEW_LIST);
        notify(message);
    }

    public void addNotify(Integer name, Notify notify) {
        if (notify != null) {
            notifyMap.put(name, notify);
        }
    }

    public void notify(NotifyMessage message) {
        if (notifyMap.size() > 0) {
            Iterator<Map.Entry<Integer, Notify>> iterator = notifyMap.entrySet().iterator();
            while(iterator.hasNext()) {
                Map.Entry<Integer, Notify> entry = iterator.next();
                if (message.getTargets().contains(entry.getKey())) {
                    entry.getValue().notify(message);
                }
            }
        }
    }

    public static NotifyPrinter getInstance() {
        return instance;
    }
}
